package com.sun.memo.web.controller;

import com.sun.memo.entity.BackLog;
import com.sun.memo.entity.User;
import com.sun.memo.service.impl.BackLogServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author wys
 **/
@Controller
public class ViewController {

    @Autowired
    BackLogServiceImpl backLogService;

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/index")
    public String toIndex(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "login";
        }
        return "index";
    }

    @RequestMapping("/tooneday")
    public String toOneday(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        List<BackLog> backLogs = backLogService.findLogsByDay("today",id);
        session.setAttribute("onedaylogs", backLogs);
        return "pages/oneday";
    }

    @RequestMapping("/toimportant")
    public String toImportant(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        List<BackLog> backLogs = backLogService.findImportantLogs(id);
        session.setAttribute("importantlogs", backLogs);
        return "pages/important";
    }

    @RequestMapping("/toplan")
    public String toPlan(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        List<BackLog> backLogs;
        backLogs = backLogService.findLogsByDay("today", id);
        session.setAttribute("todaylogs", backLogs);
        backLogs = backLogService.findLogsByDay("tomorrow",id);
        session.setAttribute("tomorrowlogs", backLogs);
        backLogs = backLogService.findLogsByDay("past",id);
        session.setAttribute("pastlogs", backLogs);
        backLogs = backLogService.findLogsByDay("thisWeek",id);
        session.setAttribute("thisWeeklogs", backLogs);
       return "pages/plan";
    }

    @RequestMapping("/totask")
    public String toTask(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        List<BackLog> backLogs = backLogService.findBackLogsById(id);
        session.setAttribute("tasklogs", backLogs);
        return "pages/task";
    }
}
